<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品品牌</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="2">
          <el-button class="goodsbut" type="primary" @click="addGoodsBrankShow"
            >添加品牌</el-button
          >
        </el-col>

        <!-- <el-col :span="2">
          <el-button
            icon="el-icon-delete"
            class="goodsbut"
            type="danger"
            @click="clearGoodsTypeCache"
            >删除缓存</el-button
          >

        </el-col> -->
      </el-row>

      <el-table :data="goodsBrankList" border stripe>
        <el-table-column type="index" label=""></el-table-column>
        <el-table-column prop="brankName" label="名称"></el-table-column>
        <el-table-column label="logo">
          <template slot-scope="scope">
            <el-image
      style="width: 100px; height: 100px"
      :src="scope.row.brankLogo"
      fit="fill"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="priority" label="优先级"></el-table-column>
        <el-table-column prop="brankDes" label="描述"></el-table-column>
        <el-table-column  label="操作">
            <template slot-scope="scope">
              <el-button
                @click="deletebyid(scope.row.id)"
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
        </el-table-column>
      </el-table>

    </el-card>

    <!-- <el-dialog
      title="修改商品品牌"
      :visible.sync="updateDialogShow"
      @close="updatDialogclose"
      width="50%"
    >
      <el-form
        :model="updatForm"
        :rules="updatFormRules"
        ref="updatFormRef"
        label-width="70px"
      >
        <el-form-item label="名称:" prop="brankName">
          <el-input v-model="addForm.brankName"></el-input>
        </el-form-item>

        <el-form-item label="logo:" prop="brankLogo">
          <el-upload
            class="avatar-uploader"
            :headers="headObj"
            :action="updateImgUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="this.addForm.brankLogo"
              :src="this.addForm.brankLogo"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="优先级:" prop="priority">
          <el-input type="number" v-model.number="addForm.priority"></el-input>
        </el-form-item>

        <el-form-item label="描述:" prop="brankDes">
          <el-input v-model="addForm.brankDes"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updatDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="updatDialogSubmit">确 定</el-button>
      </span>
    </el-dialog> -->

    <el-dialog
      title="添加商品品牌"
      :visible.sync="addDialogShow"
      @close="addDialogclose"
      width="50%"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="名称:" prop="brankName">
          <el-input v-model="addForm.brankName"></el-input>
        </el-form-item>

        <el-form-item label="logo:" prop="brankLogo">
          <el-upload
            class="avatar-uploader"
            :headers="headObj"
            :action="updateImgUrl"
            :show-file-list="false"
            :on-success="handleLogoSuccess"
          >
            <img
              v-if="this.addForm.brankLogo"
              :src="this.addForm.brankLogo"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="优先级:" prop="priority">
          <el-input type="number" v-model.number="addForm.priority"></el-input>
        </el-form-item>

        <el-form-item label="描述:" prop="brankDes">
          <el-input v-model="addForm.brankDes"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="addDialogSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoodsBrankData, addGoodsBrankData, delGoodsBrankData } from '@/api/goodsBrank'
export default {
  name: 'GoodsBrank',
  data () {
    return {
      updateImgUrl: 'http://192.168.10.1:6002/admin/dictionary/file/UploadImg',
      headObj: {
        AdminAuthorizationToken: this.$store.getters.token
      },
      addForm: {
        brankName: '',
        brankLogo: '',
        priority: 0,
        brankDes: ''
      },
      addFormRules: {
        brankName: [
          { required: true, message: '请输入商品品牌名称', trigger: 'blur' },
          {
            min: 2,
            max: 16,
            message: '长度在 2 到 16 个字符',
            trigger: 'blur'
          }
        ],
        brankLogo: [
          { required: true, message: '请添加商品品牌logo', trigger: 'blur' }
        ],
        priority: [
          { required: true, message: '请输入商品品牌顺序', trigger: 'blur' },
          { type: 'number', message: '商品品牌优先级为数字值' }
        ],
        brankDes: [
          { required: true, message: '请输入商品品牌描述', trigger: 'blur' },
          {
            min: 2,
            max: 200,
            message: '长度在 2 到 200 个字符',
            trigger: 'blur'
          }
        ]
      },
      addDialogShow: false,
      goodsBrankList: []
    }
  },
  methods: {
    addGoodsBrankShow () {
      this.addDialogShow = true
    },
    addDialogclose () {
      this.$refs.addFormRef.resetFields()
      this.addDialogShow = false
    },
    addDialogSubmit () {
      this.$refs.addFormRef.validate(
        async (valid) => {
          if (!valid) return false
          const res = await addGoodsBrankData(
            this.addForm
          )
          this.addDialogShow = false
          this.$message.success(res.data)
          this.getGoodsBrankList()
        }
      )
    },
    // updatDialogSubmit () {},
    async getGoodsBrankList () {
      const res = await getGoodsBrankData()
      this.goodsBrankList = res.data
    },
    async deletebyid (id) {
      const flag = await this.$confirm(
        '此操作将永久删除该商品品牌, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') {
        this.$message.info('取消删除商品品牌')
      } else {
        const res = await delGoodsBrankData(id)
        this.$message.success(res.data)
        this.getGoodsBrankList()
      }
    },
    handleLogoSuccess (response) {
      this.$message.success('上传成功')
      this.addForm.brankLogo = response.data
    }
  },
  created () {
    this.getGoodsBrankList()
  }
}
</script>

<style scoped lang="less">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
